<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Make Controller Kit - Firmware API: Sockets</title>
<link href="mt_doxygen.css" rel="stylesheet" type="text/css" />
<link href="../resources/mt_doxytabs.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.header {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12pt;
	color: #222222;
}
-->
</style>
</head>

<body>
<table width="800" border="0">
    <tr>
      <td colspan="3">
        <form method="get" action="http://www.google.com/search">
          <div align="right">
            <input type="text"   name="q" size="25"
              maxlength="255" value="" />
            <input type="submit" value="Search" />
            <input type="hidden" name="sitesearch" value="makingthings.com/ref/firmware/html" />
          </div>
        </form>
       </td>
    </tr>
    <tr>
      <td><div align="center"><a href="http://www.makingthings.com">
				<img src="../resources/logo_brown_medium.png" alt="MakingThings"
									width="200" height="78" border="0" align="middle" longdesc="http://www.makingthings.com" /></a></div></td>
				<td><div align="center" class="header">
      		Make Controller Kit <br />
          Firmware API
      </div></td>
      <td><div align="center"><a href="http://www.makezine.com">
					<img src="../resources/make_logo.png" alt="MAKE Zine" 
										width="164" height="73" border="0" align="middle" longdesc="http://www.makezine.com" /></a></div></td>
    </tr>
</table>
</body>
</html>
<!-- Generated by Doxygen 1.5.4 -->
<script type="text/javascript">
<!--
function changeDisplayState (e){
  var num=this.id.replace(/[^[0-9]/g,'');
  var button=this.firstChild;
  var sectionDiv=document.getElementById('dynsection'+num);
  if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){
    sectionDiv.style.display='block';
    button.src='open.gif';
  }else{
    sectionDiv.style.display='none';
    button.src='closed.gif';
  }
}
function initDynSections(){
  var divs=document.getElementsByTagName('div');
  var sectionCounter=1;
  for(var i=0;i<divs.length-1;i++){
    if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){
      var header=divs[i];
      var section=divs[i+1];
      var button=header.firstChild;
      if (button!='IMG'){
        divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild);
        button=document.createElement('img');
        divs[i].insertBefore(button,divs[i].firstChild);
      }
      header.style.cursor='pointer';
      header.onclick=changeDisplayState;
      header.id='dynheader'+sectionCounter;
      button.src='closed.gif';
      section.id='dynsection'+sectionCounter;
      section.style.display='none';
      section.style.marginLeft='14px';
      sectionCounter++;
    }
  }
}
window.onload = initDynSections;
-->
</script>
<div class="tabs">
  <ul>
    <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
    <li><a href="modules.html"><span>Modules</span></a></li>
    <li><a href="classes.html"><span>Data&nbsp;Structures</span></a></li>
    <li><a href="files.html"><span>Files</span></a></li>
    <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
  </ul>
</div>
<h1>Sockets<br>
<small>
[<a class="el" href="group___controller.html">Controller</a>]</small>
</h1>The Sockets system provides a simple interface for creating, reading and writing over both TCP and UDP.  
<a href="#_details">More...</a><table border="0" cellpadding="0" cellspacing="0">
<tr><td></td></tr>
<tr><td colspan="2"><br><h2>Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">void *&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#g7e4b76e6d4a93aa7c281e5cbba6cc584">Socket</a> (int address, int port)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Create a new TCP socket connected to the address and port specified.  <a href="#g7e4b76e6d4a93aa7c281e5cbba6cc584"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">int&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#g838d1ceaf477b3496a9fe4adf8d28147">SocketRead</a> (void *socket, char *data, int length)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Read from a TCP socket.  <a href="#g838d1ceaf477b3496a9fe4adf8d28147"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">int&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#gabf4d267c62d67bd596a22daea799662">SocketReadLine</a> (void *socket, char *data, int length)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Read a line from a TCP socket terminated by CR LF (0x0D 0x0A).  <a href="#gabf4d267c62d67bd596a22daea799662"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">int&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#g1031bc0757e65ea265deb5dc3ded3029">SocketWrite</a> (void *socket, char *data, int length)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Write to a TCP socket.  <a href="#g1031bc0757e65ea265deb5dc3ded3029"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#geab922d85a99bd7577f5e5b54628a5b4">SocketClose</a> (void *socket)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Close an existing TCP socket.  <a href="#geab922d85a99bd7577f5e5b54628a5b4"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">void *&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#g19a37ffe799e0d2de090a24a03e38cb7">ServerSocket</a> (int port)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Create a new TCP server socket and start listening for connections.  <a href="#g19a37ffe799e0d2de090a24a03e38cb7"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">void *&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#g17066cbe7e4510e8c89bba318140ff30">ServerSocketAccept</a> (void *serverSocket)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Accept an incoming connection to a ServerSocket that you have created.  <a href="#g17066cbe7e4510e8c89bba318140ff30"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">int&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#gd698a1f1aa9b0f2864056a0790417179">ServerSocketClose</a> (void *serverSocket)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Close a ServerSocket that you have created.  <a href="#gd698a1f1aa9b0f2864056a0790417179"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">void *&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce">DatagramSocket</a> (int port)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Create a socket to read and write UDP packets.  <a href="#g2cd3724da41c8c8235fa648a6fc9bcce"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">int&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#g5aa01e1403bafbfca35746bcd44a5c83">DatagramSocketSend</a> (void *datagramSocket, int address, int port, void *data, int length)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Send a UDP packet to a specified address.  <a href="#g5aa01e1403bafbfca35746bcd44a5c83"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">int&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#ge810fd22358d4358b214bb99c20f363f">DatagramSocketReceive</a> (void *datagramSocket, int incomingPort, int *address, int *port, void *data, int length)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Receive a UDP packet.  <a href="#ge810fd22358d4358b214bb99c20f363f"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group___sockets.html#g2d63455f4c8feff3399c73f70e3300b7">DatagramSocketClose</a> (void *socket)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Close a <a class="el" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket()</a>.  <a href="#g2d63455f4c8feff3399c73f70e3300b7"></a><br></td></tr>
</table>
<hr><a name="_details"></a><h2>Detailed Description</h2>
The Sockets system provides a simple interface for creating, reading and writing over both TCP and UDP. 
<p>
This subsystem is a light wrapper around LwIP, the open source TCP/IP stack used by the Make Controller Kit. There are 3 groups of socket functions:<ul>
<li>DatagramSocket - sockets for <b>UDP</b> communication</li><li>Socket - sockets for <b>TCP</b> communication</li><li>ServerSocket - sockets for accepting <b>incoming</b> TCP client connections </li></ul>
<hr><h2>Function Documentation</h2>
<a class="anchor" name="g2cd3724da41c8c8235fa648a6fc9bcce"></a><!-- doxytag: member="network.c::DatagramSocket" ref="g2cd3724da41c8c8235fa648a6fc9bcce" args="(int port)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void* DatagramSocket           </td>
          <td>(</td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>port</em>          </td>
          <td>&nbsp;)&nbsp;</td>
          <td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Create a socket to read and write UDP packets. 
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>port</em>&nbsp;</td><td>An integer specifying the port to open. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>a pointer to the socket created. </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g5aa01e1403bafbfca35746bcd44a5c83" title="Send a UDP packet to a specified address.">DatagramSocketSend( )</a>, <a class="el" href="group___sockets.html#ge810fd22358d4358b214bb99c20f363f" title="Receive a UDP packet.">DatagramSocketReceive( )</a></dd></dl>
<dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment">  <span class="comment">// create a new UDP socket on port 10101</span>
  <span class="keyword">struct </span>netconn* udpSocket = <a class="code" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket</a>( 10101 );
  <span class="comment">// now read and write to it using DatagramSocketSend( ) and DatagramSocketReceive( )</span>
</pre></div> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00386">386</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="g2d63455f4c8feff3399c73f70e3300b7"></a><!-- doxytag: member="network.c::DatagramSocketClose" ref="g2d63455f4c8feff3399c73f70e3300b7" args="(void *socket)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void DatagramSocketClose           </td>
          <td>(</td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>socket</em>          </td>
          <td>&nbsp;)&nbsp;</td>
          <td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Close a <a class="el" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket()</a>. 
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>socket</em>&nbsp;</td><td>A pointer to the <a class="el" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket()</a> to close. </td></tr>
  </table>
</dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket( )</a></dd></dl>
<dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment">  <span class="keyword">struct </span>netconn* udpSocket = <a class="code" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket</a>( 10101 ); <span class="comment">// create our socket</span>
  <span class="comment">// now close it</span>
  <a class="code" href="group___sockets.html#g2d63455f4c8feff3399c73f70e3300b7" title="Close a DatagramSocket().">DatagramSocketClose</a>( udpSocket );
</pre></div> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00516">516</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="ge810fd22358d4358b214bb99c20f363f"></a><!-- doxytag: member="network.c::DatagramSocketReceive" ref="ge810fd22358d4358b214bb99c20f363f" args="(void *datagramSocket, int incomingPort, int *address, int *port, void *data, int length)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int DatagramSocketReceive           </td>
          <td>(</td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>datagramSocket</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>incomingPort</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int *&nbsp;</td>
          <td class="paramname"> <em>address</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int *&nbsp;</td>
          <td class="paramname"> <em>port</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>data</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>length</em></td><td>&nbsp;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td><td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Receive a UDP packet. 
<p>
This function will block until a packet is received. The address and port of the sender are returned in the locations pointed to by the address and port parameters. If the incoming packet is larger than the specified size of the buffer, it will be truncated. <dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>datagramSocket</em>&nbsp;</td><td>A pointer to the <a class="el" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket()</a> you're using to read. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>incomingPort</em>&nbsp;</td><td>An integer specifying the port to listen on. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>address</em>&nbsp;</td><td>A pointer to the IP address that sent the packet. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>port</em>&nbsp;</td><td>A pointer to the port of the sender. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>data</em>&nbsp;</td><td>A pointer to the buffer to read into. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>length</em>&nbsp;</td><td>An integer specifying the number of bytes in the packet being read. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>An integer corresponding to the number of bytes successfully read. </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket( )</a></dd></dl>
<dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment">  <span class="keyword">struct </span>netconn* udpSocket = <a class="code" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket</a>( 10101 ); <span class="comment">// our socket</span>
  <span class="keywordtype">int</span> address, port;
  <span class="keywordtype">int</span> sent = <a class="code" href="group___sockets.html#ge810fd22358d4358b214bb99c20f363f" title="Receive a UDP packet.">DatagramSocketReceive</a>( udpSocket, &amp;address, &amp;port, myBuffer, myLength );
</pre></div> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00471">471</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="g5aa01e1403bafbfca35746bcd44a5c83"></a><!-- doxytag: member="network.c::DatagramSocketSend" ref="g5aa01e1403bafbfca35746bcd44a5c83" args="(void *datagramSocket, int address, int port, void *data, int length)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int DatagramSocketSend           </td>
          <td>(</td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>datagramSocket</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>address</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>port</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>data</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>length</em></td><td>&nbsp;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td><td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Send a UDP packet to a specified address. 
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>datagramSocket</em>&nbsp;</td><td>A pointer to the <a class="el" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket()</a> you're using to write. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>address</em>&nbsp;</td><td>An integer specifying the IP address to write to. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>port</em>&nbsp;</td><td>An integer specifying the port to write to. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>data</em>&nbsp;</td><td>A pointer to the packet to send. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>length</em>&nbsp;</td><td>An integer specifying the number of bytes in the packet being sent. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>An integer corresponding to the number of bytes successfully written. </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket( )</a></dd></dl>
<dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment">  <span class="keyword">struct </span>netconn* udpSocket = <a class="code" href="group___sockets.html#g2cd3724da41c8c8235fa648a6fc9bcce" title="Create a socket to read and write UDP packets.">DatagramSocket</a>( 10101 ); <span class="comment">// our socket</span>
  <span class="keywordtype">int</span> address = IP_ADDRESS( 192, 168, 0, 200 );
  <span class="keywordtype">int</span> sent = <a class="code" href="group___sockets.html#g5aa01e1403bafbfca35746bcd44a5c83" title="Send a UDP packet to a specified address.">DatagramSocketSend</a>( udpSocket, address, 10101, myBuffer, myLength );
</pre></div> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00421">421</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="g19a37ffe799e0d2de090a24a03e38cb7"></a><!-- doxytag: member="network.c::ServerSocket" ref="g19a37ffe799e0d2de090a24a03e38cb7" args="(int port)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void* ServerSocket           </td>
          <td>(</td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>port</em>          </td>
          <td>&nbsp;)&nbsp;</td>
          <td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Create a new TCP server socket and start listening for connections. 
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>port</em>&nbsp;</td><td>An integer specifying the port to listen on. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>A pointer to the socket created. </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g17066cbe7e4510e8c89bba318140ff30" title="Accept an incoming connection to a ServerSocket that you have created.">ServerSocketAccept( )</a></dd></dl>
<dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment">  <span class="comment">// create a socket and start listening on port 10101</span>
  <span class="keyword">struct </span>netconn* server = <a class="code" href="group___sockets.html#g19a37ffe799e0d2de090a24a03e38cb7" title="Create a new TCP server socket and start listening for connections.">ServerSocket</a>( 10101 );
  <span class="comment">// ServerSocketAccept( ) will block until an incoming connection is made</span>
  <span class="keyword">struct </span>netconn* newConnection = <a class="code" href="group___sockets.html#g17066cbe7e4510e8c89bba318140ff30" title="Accept an incoming connection to a ServerSocket that you have created.">ServerSocketAccept</a>( server );
  <span class="comment">// now grab the data from the new connection</span>
</pre></div> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00318">318</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="g17066cbe7e4510e8c89bba318140ff30"></a><!-- doxytag: member="network.c::ServerSocketAccept" ref="g17066cbe7e4510e8c89bba318140ff30" args="(void *serverSocket)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void* ServerSocketAccept           </td>
          <td>(</td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>serverSocket</em>          </td>
          <td>&nbsp;)&nbsp;</td>
          <td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Accept an incoming connection to a ServerSocket that you have created. 
<p>
This function will block until a new connection is waiting to be serviced. It returns a regular socket on which you can use <a class="el" href="group___sockets.html#g1031bc0757e65ea265deb5dc3ded3029" title="Write to a TCP socket.">SocketWrite()</a>, <a class="el" href="group___sockets.html#g838d1ceaf477b3496a9fe4adf8d28147" title="Read from a TCP socket.">SocketRead()</a> and <a class="el" href="group___sockets.html#geab922d85a99bd7577f5e5b54628a5b4" title="Close an existing TCP socket.">SocketClose()</a>. <dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>serverSocket</em>&nbsp;</td><td>a pointer to a ServerSocket that you created </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>a pointer to the new socket created to handle the connection </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g19a37ffe799e0d2de090a24a03e38cb7" title="Create a new TCP server socket and start listening for connections.">ServerSocket()</a>, <a class="el" href="group___sockets.html#g1031bc0757e65ea265deb5dc3ded3029" title="Write to a TCP socket.">SocketWrite()</a>, <a class="el" href="group___sockets.html#g838d1ceaf477b3496a9fe4adf8d28147" title="Read from a TCP socket.">SocketRead()</a>, <a class="el" href="group___sockets.html#geab922d85a99bd7577f5e5b54628a5b4" title="Close an existing TCP socket.">SocketClose()</a> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00342">342</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="gd698a1f1aa9b0f2864056a0790417179"></a><!-- doxytag: member="network.c::ServerSocketClose" ref="gd698a1f1aa9b0f2864056a0790417179" args="(void *serverSocket)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int ServerSocketClose           </td>
          <td>(</td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>serverSocket</em>          </td>
          <td>&nbsp;)&nbsp;</td>
          <td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Close a ServerSocket that you have created. 
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>serverSocket</em>&nbsp;</td><td>A pointer to a ServerSocket. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>0 if the process was successful. </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g19a37ffe799e0d2de090a24a03e38cb7" title="Create a new TCP server socket and start listening for connections.">ServerSocket()</a></dd></dl>
<dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment">  <span class="comment">// we created a server socket at some point</span>
  <span class="keyword">struct </span>netconn* server = <a class="code" href="group___sockets.html#g19a37ffe799e0d2de090a24a03e38cb7" title="Create a new TCP server socket and start listening for connections.">ServerSocket</a>( 10101 );
  <span class="comment">// now close it</span>
  <a class="code" href="group___sockets.html#gd698a1f1aa9b0f2864056a0790417179" title="Close a ServerSocket that you have created.">ServerSocketClose</a>( server );
</pre></div> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00366">366</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="g7e4b76e6d4a93aa7c281e5cbba6cc584"></a><!-- doxytag: member="network.c::Socket" ref="g7e4b76e6d4a93aa7c281e5cbba6cc584" args="(int address, int port)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void* Socket           </td>
          <td>(</td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>address</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>port</em></td><td>&nbsp;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td><td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Create a new TCP socket connected to the address and port specified. 
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>address</em>&nbsp;</td><td>An integer specifying the IP address to connect to. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>port</em>&nbsp;</td><td>An integer specifying the port to connect on. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>A pointer to the socket, if it was created successfully. NULL if unsuccessful. </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g838d1ceaf477b3496a9fe4adf8d28147" title="Read from a TCP socket.">SocketRead()</a>, <a class="el" href="group___sockets.html#g1031bc0757e65ea265deb5dc3ded3029" title="Write to a TCP socket.">SocketWrite()</a>, <a class="el" href="group___sockets.html#geab922d85a99bd7577f5e5b54628a5b4" title="Close an existing TCP socket.">SocketClose()</a></dd></dl>
<dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment">  <span class="comment">// use the IP_ADDRESS macro to format the address properly</span>
  <span class="keywordtype">int</span> addr = IP_ADDRESS( 192, 168, 0, 54 );
  <span class="comment">// then create the socket, connecting on port 10101</span>
  <span class="keyword">struct </span>netconn* socket = <a class="code" href="group___sockets.html#g7e4b76e6d4a93aa7c281e5cbba6cc584" title="Create a new TCP socket connected to the address and port specified.">Socket</a>( addr, 10101 );
</pre></div> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00109">109</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="geab922d85a99bd7577f5e5b54628a5b4"></a><!-- doxytag: member="network.c::SocketClose" ref="geab922d85a99bd7577f5e5b54628a5b4" args="(void *socket)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void SocketClose           </td>
          <td>(</td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>socket</em>          </td>
          <td>&nbsp;)&nbsp;</td>
          <td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Close an existing TCP socket. 
<p>
Anytime you get an error when trying to read or write, it's best to close the socket and reopen it to make sure that the connection is corrently configured. <dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>socket</em>&nbsp;</td><td>A pointer to the existing socket. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>void </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g7e4b76e6d4a93aa7c281e5cbba6cc584" title="Create a new TCP socket connected to the address and port specified.">Socket()</a></dd></dl>
<dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment">  <span class="comment">// we should already have created a socket 'sock' with Socket().</span>
  <span class="keyword">struct </span>netconn* sock = <a class="code" href="group___sockets.html#g7e4b76e6d4a93aa7c281e5cbba6cc584" title="Create a new TCP socket connected to the address and port specified.">Socket</a>( addr, 10101 );
  <span class="comment">// now close it</span>
  <a class="code" href="group___sockets.html#geab922d85a99bd7577f5e5b54628a5b4" title="Close an existing TCP socket.">SocketClose</a>( sock )
</pre></div> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00288">288</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="g838d1ceaf477b3496a9fe4adf8d28147"></a><!-- doxytag: member="network.c::SocketRead" ref="g838d1ceaf477b3496a9fe4adf8d28147" args="(void *socket, char *data, int length)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int SocketRead           </td>
          <td>(</td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>socket</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">char *&nbsp;</td>
          <td class="paramname"> <em>data</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>length</em></td><td>&nbsp;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td><td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Read from a TCP socket. 
<p>
Make sure you have an open socket before trying to read from it. <dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>socket</em>&nbsp;</td><td>A pointer to the existing socket. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>data</em>&nbsp;</td><td>A pointer to the buffer to read to. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>length</em>&nbsp;</td><td>An integer specifying the maximum length in bytes that can be read. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>An integer: length of data read if successful, zero on failure. </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g7e4b76e6d4a93aa7c281e5cbba6cc584" title="Create a new TCP socket connected to the address and port specified.">Socket()</a>, <a class="el" href="group___sockets.html#geab922d85a99bd7577f5e5b54628a5b4" title="Close an existing TCP socket.">SocketClose()</a></dd></dl>
<dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment">  <span class="comment">// we should already have created a socket sock with Socket().</span>
  <span class="keyword">struct </span>netconn* sock = <a class="code" href="group___sockets.html#g7e4b76e6d4a93aa7c281e5cbba6cc584" title="Create a new TCP socket connected to the address and port specified.">Socket</a>( addr, 10101 );
  <span class="keywordtype">int</span> length_read = <a class="code" href="group___sockets.html#g838d1ceaf477b3496a9fe4adf8d28147" title="Read from a TCP socket.">SocketRead</a>( sock, data, length )
  <span class="comment">// if 0 bytes were read, there was some sort of error</span>
  if( length_read == 0 )
    <a class="code" href="group___sockets.html#geab922d85a99bd7577f5e5b54628a5b4" title="Close an existing TCP socket.">SocketClose</a>( sock );
</pre></div> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00156">156</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="gabf4d267c62d67bd596a22daea799662"></a><!-- doxytag: member="network.c::SocketReadLine" ref="gabf4d267c62d67bd596a22daea799662" args="(void *socket, char *data, int length)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int SocketReadLine           </td>
          <td>(</td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>socket</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">char *&nbsp;</td>
          <td class="paramname"> <em>data</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>length</em></td><td>&nbsp;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td><td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Read a line from a TCP socket terminated by CR LF (0x0D 0x0A). 
<p>
Make sure you have an open socket before trying to read from it. <dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>socket</em>&nbsp;</td><td>A pointer to the existing socket. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>data</em>&nbsp;</td><td>A pointer to the buffer to read to. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>length</em>&nbsp;</td><td>An integer specifying the maximum length in bytes to read. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>An integer: length of data read if successful, zero on failure. </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g7e4b76e6d4a93aa7c281e5cbba6cc584" title="Create a new TCP socket connected to the address and port specified.">Socket()</a>, <a class="el" href="group___sockets.html#g838d1ceaf477b3496a9fe4adf8d28147" title="Read from a TCP socket.">SocketRead()</a>, <a class="el" href="group___sockets.html#geab922d85a99bd7577f5e5b54628a5b4" title="Close an existing TCP socket.">SocketClose()</a> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00216">216</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<a class="anchor" name="g1031bc0757e65ea265deb5dc3ded3029"></a><!-- doxytag: member="network.c::SocketWrite" ref="g1031bc0757e65ea265deb5dc3ded3029" args="(void *socket, char *data, int length)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int SocketWrite           </td>
          <td>(</td>
          <td class="paramtype">void *&nbsp;</td>
          <td class="paramname"> <em>socket</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">char *&nbsp;</td>
          <td class="paramname"> <em>data</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&nbsp;</td>
          <td class="paramname"> <em>length</em></td><td>&nbsp;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td><td width="100%"></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>
Write to a TCP socket. 
<p>
Not surprisingly, we need an existing socket before we can write to it.<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>socket</em>&nbsp;</td><td>A pointer to the existing socket. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>data</em>&nbsp;</td><td>A pointer to the buffer to write from. </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>length</em>&nbsp;</td><td>An integer specifying the length in bytes of how much data should be written. </td></tr>
  </table>
</dl>
<dl class="return" compact><dt><b>Returns:</b></dt><dd>An integer: 'length written' if successful, 0 on failure. </dd></dl>
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="group___sockets.html#g7e4b76e6d4a93aa7c281e5cbba6cc584" title="Create a new TCP socket connected to the address and port specified.">Socket()</a></dd></dl>
<dl class="user" compact><dt><b>Example</b></dt><dd><div class="fragment"><pre class="fragment">  <span class="comment">// we should already have created a socket with Socket()</span>
  <span class="keyword">struct </span>netconn* sock = <a class="code" href="group___sockets.html#g7e4b76e6d4a93aa7c281e5cbba6cc584" title="Create a new TCP socket connected to the address and port specified.">Socket</a>( addr, 10101 );
  <span class="keywordtype">int</span> length_written = <a class="code" href="group___sockets.html#g1031bc0757e65ea265deb5dc3ded3029" title="Write to a TCP socket.">SocketWrite</a>( sock, data, length )
  <span class="comment">// if 0 bytes were written, there was some sort of error</span>
  if( length_written == 0 )
    <a class="code" href="group___sockets.html#geab922d85a99bd7577f5e5b54628a5b4" title="Close an existing TCP socket.">SocketClose</a>( sock );
</pre></div> </dd></dl>

<p>Definition at line <a class="el" href="network_8c-source.html#l00263">263</a> of file <a class="el" href="network_8c-source.html">network.c</a>.</p>

</div>
</div><p>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Make Controller Kit</title>
<style type="text/css">
<!--
.footer {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 9pt;
	color: #999999;
}
-->
</style>
</head>

<body>
<hr size="1">
<div>
  <p class="footer">The Make Controller Kit is an open source project maintained by <a href="http://www.makingthings.com">MakingThings</a>.<br />
    MakingThings code is released under the <a href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank">Apache 2.0 license</a>.<br />
    Bug tracker, development wiki and status can be found at <a href="http://dev.makingthings.com">http://dev.makingthings.com</a>.<br />
		This document was last updated on 5 Dec 2007.
</p>
</div>
<!-- Google Analytics Code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-769030-1";
urchinTracker();
</script>
</body>
</html>
